package com.casic.modules.sys.controller;

import com.casic.common.annotation.SysLog;
import com.casic.common.base.entity.PageEntity;
import com.casic.common.utils.R;
import com.casic.modules.sys.entity.VO.SysActiveUserVo;
import com.casic.modules.sys.shiro.RedisShiroSessionDAO;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created by jack on 2018/6/29.
 */
@RestController
@RequestMapping("/sys/activeuser")
public class SysActiveUsserController {
    @Autowired
    private SessionDAO sessionDAO;


    @Autowired
     private RedisShiroSessionDAO redisShiroSessionDAO;

    @Value("${renren.redis.open}")
    private boolean redisOpen;

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        int currentPage = 1;
        int pageSize = 10;
        if (params.get("page") != null) {
            currentPage = Integer.parseInt((String) params.get("page"));
        }
        if (params.get("limit") != null) {
            pageSize = Integer.parseInt((String) params.get("limit"));
        }
        Collection<Session> sessions = sessionDAO.getActiveSessions();
        if(redisOpen){
            sessions=redisShiroSessionDAO.getActiveSessions();
        }
        ArrayList<Session> list2 = new ArrayList();
        for (Iterator<Session> iterator = sessions.iterator(); iterator.hasNext(); ) {
            Session s1 = iterator.next();
            s1.setAttribute("user",s1.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY));
            list2.add(s1);
        }

        List users = new ArrayList<Session>();
        for (int i = (currentPage - 1) * pageSize; i < currentPage * pageSize; i++) {
            if (i >= sessions.size()) {
                break;
            }
            Session session = list2.get(i);
            SysActiveUserVo sysActiveUserVo=new SysActiveUserVo();
         //   users.add(session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY));
            users.add(session);
        }
        PageEntity<SysActiveUserVo> list = new PageEntity<SysActiveUserVo>(users);

        list.setCurrPage(currentPage);
        list.setPageSize(pageSize);
        list.setIsFirstPage(currentPage == 0 || currentPage == 1);
        list.setIsLastPage(currentPage == sessions.size());
        list.setTotalCount(sessions.size());
        list.setTotalPage(sessions.size() % pageSize > 0 ? (sessions.size() / pageSize + 1) : sessions.size() / pageSize);
        return R.ok().put("page", list);
    }



    @SysLog("剔出用户")
    @RequestMapping("/delete")
    public R delete( String sessionId){

        Collection<Session> sessions = sessionDAO.getActiveSessions();
        if(redisOpen){
            sessions=redisShiroSessionDAO.getActiveSessions();
            for(Session session :sessions){
                if( sessionId.equals(session.getId()) ){
                    redisShiroSessionDAO.delete(session);
                }
            }
        }else{
            for(Session session :sessions){
                if( sessionId.equals(session.getId()) ){
                    sessionDAO.delete(session);
                }
            }
        }

        return R.ok();
    }
}

